package com.grad.why.studentsystem.controller.qlx;

import com.grad.why.studentsystem.controller.StudentArriveInfoController;
import com.grad.why.studentsystem.domain.Runtimecoursearrangeinforecord;
import com.grad.why.studentsystem.domain.User;
import com.grad.why.studentsystem.domain.current.CurrentChooseCourseInfo;
import com.grad.why.studentsystem.domain.current.CurrentCourse;
import com.grad.why.studentsystem.service.RuntimeCourseArrangeInfoRecordService;
import com.grad.why.studentsystem.service.qlxservice.TeacherInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author tyh
 * @Package com.grad.why.studentsystem.controller
 * @Description:
 * @date 19-5-17 上午10:47
 */
@Controller
@RequestMapping("/studentManageSystem")
public class TeacherInfoController {

    @Autowired
    TeacherInfoService teacherInfoService;

    @Autowired
    StudentArriveInfoController studentArriveInfoController;

    @Autowired
    RuntimeCourseArrangeInfoRecordService runtimeCourseArrangeInfoRecordService;

    @ResponseBody
    @RequestMapping("/getTeacherInfo")
    public Object getTeacherInfo(HttpSession session){
        User user=(User)session.getAttribute("user");
        int bindId=user.getBindid();
        Map<String,Object> map=new LinkedHashMap<>();
        map.put("data",teacherInfoService.getTeacherInfo(bindId));
        return map;
    }

    @ResponseBody
    @RequestMapping("/getSelectTeacherCourseInfo")
    public Object getSelectTeacherCourseInfo(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "order") String order,
            HttpSession session){
        try {
            User user=(User)session.getAttribute("user");
            int bindId=user.getBindid();
            List<CurrentChooseCourseInfo> rows = teacherInfoService.getSelectTeacherCourseInfo(offset, limit, bindId);
            long total = teacherInfoService.getSelectTeacherCourseNum(bindId);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @ResponseBody
    @RequestMapping("/getTeacherCourseInfo")
    public Object getTeacherCourseInfo(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            HttpSession session){
        try {
            User user=(User)session.getAttribute("user");
            int bindId=user.getBindid();
            List<CurrentCourse> rows = teacherInfoService.getTeacherCourseInfo(offset, limit, bindId);
            long total = teacherInfoService.getTeacherCourseNum(bindId);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @ResponseBody
    @RequestMapping("/getTeacherCourseStudent")
    public Object getTeacherCourseStudent(@RequestBody Map<String,Object> map,HttpSession session){
        int courseId=Integer.parseInt(String.valueOf(map.get("id")));
        User user=(User)session.getAttribute("user");
        int bindId=user.getBindid();
        return teacherInfoService.getTeacherCourseStudent(courseId,bindId);
    }

    @RequestMapping("/getCourseArriveStudentInfo")
    @ResponseBody
    public Object getCourseArriveStudentInfo(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order,
            HttpSession session
    ){
        User user=(User)session.getAttribute("user");
        int bindId=user.getBindid();
        long total=runtimeCourseArrangeInfoRecordService.getStudentArriveInfoListLength("");
        Map<String,Object> map=(Map<String,Object>)studentArriveInfoController.getProjectInfos(0,(int)total,"","","");
        List<Runtimecoursearrangeinforecord> rows=(List<Runtimecoursearrangeinforecord>)map.get("rows");
        Iterator<Runtimecoursearrangeinforecord> iterator=rows.iterator();
        List<Map<String,Object>> list=new ArrayList<>();
        while (iterator.hasNext()){
            Runtimecoursearrangeinforecord data=iterator.next();
            if (data.getTeacherid()==bindId){
                Map<String,Object> stuInfo=new HashMap<>();
                stuInfo.put("recordid",data.getRecordid());
                stuInfo.put("coursearrangeid",data.getCoursearrangeid());
                stuInfo.put("classroomid",data.getClassroomid());
                stuInfo.put("classroomname",data.getClassroomname());
                stuInfo.put("shouldcount",data.getShouldcount());
                stuInfo.put("shouldnamelabels",data.getShouldnamelabels());
                stuInfo.put("courseindex",data.getCourseindex());
                stuInfo.put("startweek",data.getStartweek());
                stuInfo.put("endweek",data.getEndweek());
                stuInfo.put("cid",data.getCid());
                stuInfo.put("coursename",data.getCoursename());
                stuInfo.put("weekday",data.getWeekday());
                stuInfo.put("teacherid",data.getTeacherid());
                stuInfo.put("teachername",data.getTeachername());
                stuInfo.put("realcount",data.getRealcount());
                stuInfo.put("realnamelabels",data.getRealnamelabels());
                stuInfo.put("recordtime",data.getRecordtime());

                stuInfo.put("arriverate",(data.getRealcount()*1.0)/(data.getShouldcount()*1.0));
                list.add(stuInfo);
            }
        }
        List<Map<String,Object>> data=new ArrayList<>();
        int min=offset*limit;
        int max=min+limit;
        max=max>list.size()?list.size():max;
        for (int i=min;i<max;i++){
            data.add(list.get(i));
        }
        Map<String,Object> returnData=new HashMap<>();
        returnData.put("rows",data);
        returnData.put("total",list.size());
        return returnData;
    }
}
